#include "sem.h"
#include "stdio.h"

void do_semaphore_init(semaphore_t *s, int val)
{
    s->value = val;
    queue_init(&(s->waiting_queue));
}

void do_semaphore_up(semaphore_t *s)
{
    s->value++;
    if (s->value <= 0)
    {
        pcb_t *first_task;
        first_task = queue_dequeue(&(s->waiting_queue));
        first_task->status = TASK_READY;
        queue_push(&ready_queue, first_task);
    }
}

void do_semaphore_down(semaphore_t *s)
{
    s->value--;
    if (s->value < 0)
    {
        current_running[core_id]->status = TASK_BLOCKED;
        queue_push(&(s->waiting_queue), current_running[core_id]);
        do_scheduler();
    }
}